package com.yami.shop.listener;

import com.yami.shop.bean.app.param.ProdCommParam;
import com.yami.shop.bean.event.OrderSettlementEvent;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.order.ConfirmOrderOrder;
import com.yami.shop.service.OrderItemService;
import com.yami.shop.service.ProdCommService;
import com.yami.shop.service.ShopDetailService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @title: OrderCommentListener
 * @Author Chiley
 * @Date: 2023/3/10 11:34
 */

@Component("orderCommentListener")
@AllArgsConstructor
public class OrderCommentListener {

    private final ShopDetailService shopDetailService;
    private final ProdCommService prodCommService;
    private final OrderItemService orderItemService;

    /**
     * 订单确认收货后过了维权期的订单结算操作
     *
     * @param event
     */
    @EventListener(OrderSettlementEvent.class)
    @Order(ConfirmOrderOrder.DEFAULT)
    public void defaultCommentOrderEvent(OrderSettlementEvent event) {
        List<com.yami.shop.bean.model.Order> orderList = event.getOrder();
        List<String> orderNumberList = orderList.stream().map(com.yami.shop.bean.model.Order::getOrderNumber).collect(Collectors.toList());
        orderNumberList.forEach(orderNumber -> {
            List<OrderItem> orderItems = orderItemService.listAndPayTimeByOrderNumber(orderNumber);
            orderItems.forEach(orderItem -> {
                ProdCommParam prodCommParam = new ProdCommParam();
                prodCommParam.setScore(5);
                prodCommParam.setContent("系统默认好评");
                prodCommParam.setIsAnonymous(1);
                prodCommParam.setRecTime(new Date());
                prodCommService.comm(orderItem, prodCommParam);
            });
        });
        List<Long> shopIdList = orderList.stream().map(com.yami.shop.bean.model.Order::getShopId).collect(Collectors.toList());
        shopIdList.forEach(shopDetailService::computeShopScore);
    }
}
